package com.hzcx.app.simplechat.ui.user;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.ui.user.bean.UserInfoBean;
import com.hzcx.app.simplechat.ui.user.contract.UserRemarkContract;
import com.hzcx.app.simplechat.ui.user.event.UserLableEvent;
import com.hzcx.app.simplechat.ui.user.event.UserRemarkEvent;
import com.hzcx.app.simplechat.ui.user.presenter.UserRemarkPresenter;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 用户备注界面
 */
public class UserRemarkActivity extends BaseActivity<UserRemarkPresenter> implements UserRemarkContract.View {

    public static final String INTENT_USER_INFO = "INTENT_USER_INFO";

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.et_remark)
    EditText etRemark;
    @BindView(R.id.et_des)
    EditText etDes;
    @BindView(R.id.tv_lable)
    TextView tvLable;

    private UserInfoBean userInfoBean;

    @Override
    public int getLayout() {
        return R.layout.activity_user_remark;
    }

    @Override
    public void initPresenter() {
        mPresenter = new UserRemarkPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText("设置备注");
        tvTitleRight.setText("完成");
    }

    @Override
    public void initData() {
        userInfoBean = (UserInfoBean) getIntent().getSerializableExtra(INTENT_USER_INFO);

        if (userInfoBean != null) {
            if (EmptyUtils.isNotEmpty(userInfoBean.getRemarks_name())) {
                etRemark.setText(userInfoBean.getRemarks_name());
            }
            if (EmptyUtils.isNotEmpty(userInfoBean.getDescription())) {
                etDes.setText(userInfoBean.getDescription());
            }
            setLableNames();
        }
    }

    @OnClick({R.id.tv_title_right, R.id.tv_lable})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_title_right: // 完成
                mPresenter.setRemarkAndDes(this, userInfoBean.getMember_id(), etRemark.getText().toString(), etDes.getText().toString());
                break;

            case R.id.tv_lable: // 用户标签设置
                startActivity(new Intent(this, UserLableSettingActivity.class)
                        .putExtra(UserLableSettingActivity.INTENT_USERINFO, userInfoBean));
                break;
        }
    }

    /**
     * 设置成功
     */
    @Override
    public void setSuccess() {
        showError("设置成功");
        userInfoBean.setRemarks_name(etRemark.getText().toString());
        userInfoBean.setDescription(etDes.getText().toString());
        EventBus.getDefault().post(new UserRemarkEvent(userInfoBean));
        finish();
    }

    /**
     * 用户标签修改成功
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void userLableSuccess(UserLableEvent event) {
        try {
            if (userInfoBean.getLable_name_arr() == null) {
                userInfoBean.setLable_name_arr(new ArrayList<>());
            }
            userInfoBean.getLable_name_arr().clear();
            userInfoBean.getLable_name_arr().addAll(event.getData());
            setLableNames();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置标签名称
     */
    private void setLableNames() {
        try {
            String names = "";
            if (userInfoBean != null && userInfoBean.getLable_name_arr() != null && userInfoBean.getLable_name_arr().size() > 0) {
                for (int i = 0; i < userInfoBean.getLable_name_arr().size(); i++) {
                    if (userInfoBean.getLable_name_arr().get(i) == null) {
                        continue;
                    }
                    if (i == 0) {
                        names = userInfoBean.getLable_name_arr().get(i).getName();
                    } else {
                        names = names + "," + userInfoBean.getLable_name_arr().get(i).getName();
                    }
                }
            }
            tvLable.setText(names);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
